// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.prefs.connectivity.models;

import java.io.File;
import java.util.Properties;

import org.dengues.commons.connectivity.EDatabaseType;
import org.dengues.commons.connectivity.IConnectivitySettingsConstants;
import org.dengues.core.i18n.Messages;
import org.dengues.core.prefs.connectivity.EConnectivityProperties;
import org.dengues.core.prefs.connectivity.XMLFileManager;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ConnectivityProperty extends ConnectivityCategory {

    private String name;

    private String id;

    private Properties baseProperties;

    private boolean isValid;

    private String statusMessage;

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectivityProperty constructor comment.
     * 
     * @param parent
     */
    private ConnectivityProperty(ConnectivityCategory parent) {
        if (parent != null) {
            setParent(parent);
            setImage(parent.getImage());
            setType(parent.getType());
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectivityProperty constructor comment.
     * 
     * @param parent
     * @param name
     */
    public ConnectivityProperty(ConnectivityCategory parent, String name) {
        this(parent);
        this.name = name;
        setId(ConnectivityProvider.getInstance().getCategoryId(this));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectivityProperty constructor comment.
     * 
     * @param object
     * @param name2
     * @param id2
     */
    public ConnectivityProperty(String name2, String id2) {
        this(ConnectivityProvider.getInstance().getCategoriesById(id2), name2);
        this.id = id2;
    }

    /**
     * Getter for name.
     * 
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Sets the name.
     * 
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Getter for id.
     * 
     * @return the id
     */
    @Override
    public String getId() {
        return this.id;
    }

    /**
     * Sets the id.
     * 
     * @param id the id to set
     */
    @Override
    public void setId(String id) {
        this.id = id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public ConnectivityProperty clone() {
        ConnectivityProperty clone = null;
        try {
            clone = (ConnectivityProperty) super.clone();
            Properties properties = (Properties) getBaseProperties().clone();
            clone.setBaseProperties(properties);
        } catch (CloneNotSupportedException ex) {
            ex.printStackTrace();
        }
        return clone;
    }

    /**
     * Getter for baseProperties.
     * 
     * @return the baseProperties
     */
    public Properties getBaseProperties() {
        return this.baseProperties;
    }

    /**
     * Sets the baseProperties.
     * 
     * @param baseProperties the baseProperties to set
     */
    public void setBaseProperties(Properties baseProperties) {
        this.baseProperties = baseProperties;
    }

    /**
     * Getter for isValid.
     * 
     * @return the isValid
     */
    public boolean isValid() {
        return this.isValid;
    }

    /**
     * Sets the isValid.
     * 
     * @param isValid the isValid to set
     */
    public void setValid(boolean isValid) {
        this.isValid = isValid;
    }

    /**
     * Getter for statusMessage.
     * 
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return this.statusMessage;
    }

    /**
     * Sets the statusMessage.
     * 
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public void validate() {
        if (getBaseProperties() == null) {
            setValid(false);
            setStatusMessage(Messages.getString("ConnectivityProperty.notSetted")); //$NON-NLS-1$
        } else if (getBaseProperties().get(IConnectivitySettingsConstants.PROP_DEFN_JARLIST) == null
                || getBaseProperties().get(IConnectivitySettingsConstants.PROP_DEFN_JARLIST).equals("")) { //$NON-NLS-1$
            String driveClass = getBaseProperties().getProperty(EConnectivityProperties.PROP_ID_DRIVER.getPropId());
            setValid(driveClass.equals(EDatabaseType.GJDBC.getDriver()));
            if (isValid) {
                setStatusMessage(Messages.getString("ConnectivityProperty.okStatus")); //$NON-NLS-1$
            } else {
                setStatusMessage(Messages.getString("ConnectivityProperty.emptyJars")); //$NON-NLS-1$
            }
        } else {
            String[] parseString = XMLFileManager.parseString((String) getBaseProperties().get(
                    IConnectivitySettingsConstants.PROP_DEFN_JARLIST));
            StringBuilder builder = new StringBuilder(Messages.getString("ConnectivityProperty.unableLocation")); //$NON-NLS-1$
            boolean b = true;
            for (String string : parseString) {
                File file = new File(string);
                if (!file.exists()) {
                    setValid(false);
                    b = false;
                    builder.append(string);
                }
            }
            if (b) {
                setValid(true);
                setStatusMessage(Messages.getString("ConnectivityProperty.okStatus")); //$NON-NLS-1$
            } else {
                setStatusMessage(builder.toString());
            }
        }
    }
}
